#!/usr/bin/python

#Wordpress /wp-includes/version.php

import os
import re


def cmpver(a, b):
	def fixup(i):
		try:
			return int(i)
		except ValueError:
			return i
	a = map(fixup, re.findall("\d+|\w+", a))
	b = map(fixup, re.findall("\d+|\w+", b))
	return cmp(a, b)

wordpress_current_version='3.2.1'
wordpress=0
wordpress_outdated=0

for top, dirs, files in os.walk('/home'):
	for basename in files:
		file=os.path.join(top, basename);

		# Wordpress
		if file.endswith('/wp-includes/version.php'):
			wordpress=wordpress+1
			version=''
			for line in open(file):
				if line.startswith('$wp_version'):
					version=line[line.find('\'')+1:line.rfind('\'')]
					break
			version_text=version
			if cmpver(wordpress_current_version, version):
				wordpress_outdated=wordpress_outdated+1
				version_text='\033[31m'+version+'\033[0m'
			print "Wordpress %s: %s" % (version_text, os.path.dirname(os.path.dirname(file)))

print "Summary:"
print "Wordpress:   %s" % wordpress
print " -outdated:  %s" % wordpress_outdated
print " -updated:   %s" % wordpress-wordpress_outdated

